const result = query(list)
.where(item => item.age > 18)
.sortBy('id')
.groupBy('name')
.execute();
console.log(result);
const list = [ { name: 'John', id: 1, age: 25 }, { name: 'Alice', id: 2, age: 30 }, { name: 'Bob', id: 3, age: 28 }, { name: 'Emily', id: 4, age: 22 }, { name: 'Michael', id: 5, age: 32 }, { name: 'Sara', id: 6, age: 27 }, { name: 'David', id: 7, age: 35 }, { name: 'Olivia', id: 8, age: 29 }, { name: 'Daniel', id: 9, age: 31 }, { name: 'Sophia', id: 10, age: 26 } ];
function query(list){
let data = [...list];
function where(callback){
const temp = data.filter(callback)
return query(temp)
}
function sortBy(key){
const temp = data.sort((a,b)=>a[key]-b[key])
return query(temp)
}
function groupBy(key){
let temp = {}
data.forEach(item => {
const value = item[key];
if (!temp[value]) {
temp[value] = [];
}
temp[value].push(item);
});
return query([temp])
}
function execute(){
return data;
}
return {
where,
sortBy,
groupBy,
execute
}
}